/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid;

import com.google.gwt.dom.client.Element;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.DOM;

import java.io.Serializable;
import java.util.Date;

public class Utils {

	public static Date nextDay(Date dt) {
		final long MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
		return dt == null ? null : new Date(dt.getTime() + MILLIS_IN_DAY);
	}

	/**
	 * Format used to pass Date between client and server to
	 * avoid convering from GMT to locale.
	 */
	public static final String dateFormat = "yyyy-MM-dd";

	/**
	 * Button styles
	 */
	public final static String blueButton = "bluebutton";
	public final static String greenButton = "greenbutton";
	public final static String redButton = "redbutton";
	public final static String grayButton = "greybutton";

	public final static String EMAIL_REGEXP = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";

	/**
	 * Validates email
	 * GWT UI copability
	 * 
	 * @param email
	 * @return <code>true</code> if email is valid
	 */
	public static boolean isEmailValid(String email) {
		if (email == null || email.trim().isEmpty()) {
			return false;
		}
		RegExp regex = RegExp.compile(EMAIL_REGEXP, "i");
		return regex.test(email.trim());
	}

	public static boolean isDomainValid(String domain) {
		if (domain == null) {
			return false;
		}
		domain = domain.trim();
		if (domain.isEmpty()) {
			return false;
		}
		if (domain.length() < 3) {
			return false;
		}
		if (!domain.contains(".")) {
			return false;
		}
		if (domain.contains(" ")) {
			return false;
		}
		if (domain.indexOf(".") == 0) {
			return false;
		}
		if (domain.lastIndexOf(".") == domain.length() - 1) {
			return false;
		}
		if (domain.contains("@")) {
			return false;
		}
		return true;
	}

	public static String getTextFromHtml(String html) {
		Element element = DOM.createDiv();
		element.setInnerHTML(html);
		return element.getInnerText();
	}

	public static boolean hasClassName(com.google.gwt.dom.client.Element element, String className) {
		String oldStyle = element.getClassName();
		int idx = oldStyle.indexOf(className);

		// Calculate matching index.
		while (idx != -1) {
			if (idx == 0 || oldStyle.charAt(idx - 1) == ' ') {
				int last = idx + className.length();
				int lastPos = oldStyle.length();
				if ((last == lastPos) || ((last < lastPos) && (oldStyle.charAt(last) == ' '))) {
					break;
				}
			}
			idx = oldStyle.indexOf(className, idx + 1);
		}
		return idx != -1;
	}

	public static String getStringFromSerializable(Serializable value) {
		if (value != null) {
			if (value instanceof String) {
				return (String) value;
			} else {
				try {
					return value.toString();
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	public static Long getLongFromString(String value) {
		try {
			return Long.parseLong(value);
		} catch (Exception e) {
			return null;
		}
	}

}
